package com.kzj.fun.interfaces.impl;

import com.kzj.common.config.ConfigProperties;
import com.kzj.common.log.Logger;
import com.kzj.fun.container.ImageBox;
import com.kzj.fun.container.root.RootPanel;
import com.kzj.fun.enums.PaintType;
import com.kzj.fun.interfaces.ImageBoxDispose;
import com.kzj.fun.interfaces.MenuClickListener;
import com.kzj.fun.interfaces.MenuBarFunction;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class MenuBarFunctionImpl implements MenuBarFunction {
    private RootPanel rootPanel;
    public MenuBarFunctionImpl(RootPanel rootPanel){
        this.rootPanel=rootPanel;
    }
    @Override
    public MenuClickListener save() {
        Color color=Color.RED;
        int stroke=5;
        return (MenuClickListener) e -> {
            Logger.debug("点击保存");
            String fileName = ConfigProperties.get("savePicturePrefix") + System.currentTimeMillis() + ".png";
            File file = new File(ConfigProperties.get("savePath") + "/" + fileName);
            BufferedImage subImage=rootPanel.imageHistory.peek().getSubimage(rootPanel.rect.getX(),rootPanel.rect.getY(),rootPanel.rect.getWidth(),rootPanel.rect.getHeight());
            try {
                ImageIO.write(subImage, "png", file);
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
            Logger.info(String.format("图片: %s 保存成功", fileName));
            rootPanel.menuBar.transferFocusUpCycle();
        };

    }

    @Override
    public MenuClickListener cancel() {
        return e->{
            Logger.debug("点击取消");
            rootPanel.menuBar.transferFocusUpCycle();
            rootPanel.cancelSelectBox();
        };
    }

    @Override
    public MenuClickListener confirm() {
        return e->{
            Logger.debug("点击确认");
            rootPanel.menuBar.transferFocusUpCycle();
            rootPanel.cancelSelectBox();
            BufferedImage subImage=rootPanel.imageHistory.peek().getSubimage(rootPanel.rect.getX(),rootPanel.rect.getY(),rootPanel.rect.getWidth(),rootPanel.rect.getHeight());
            ImageBox imageBox = new ImageBox(new Point(rootPanel.rect.getX(), rootPanel.rect.getY()), new Point(rootPanel.rect.getX() + rootPanel.rect.getWidth(), rootPanel.rect.getY() + rootPanel.rect.getHeight()), subImage);
            rootPanel.imageBoxList.add(imageBox);
            imageBox.setImageBoxDispose(new ImageBoxDispose() {
                @Override
                public void before() {
                    rootPanel.remove(imageBox);
                }

                @Override
                public void after() {

                }
            });
            rootPanel.father.iconified();
        };
    }

    @Override
    public MenuClickListener paintBrush() {
        return e->{
            if(rootPanel.paintType==PaintType.PAINT_BRUSH){
                rootPanel.paintType=PaintType.NULL;
            }else rootPanel.paintType=PaintType.PAINT_BRUSH;
            Logger.debug("点击画笔 start");
        };
    }

    @Override
    public MenuClickListener line() {
        return e->{
            if(rootPanel.paintType==PaintType.LINE){
                rootPanel.paintType=PaintType.NULL;
            }else rootPanel.paintType= PaintType.LINE;
        };
    }

    @Override
    public MenuClickListener rect() {
        return  e->{
            if(rootPanel.paintType==PaintType.RECT){
                rootPanel.paintType=PaintType.NULL;
            }else rootPanel.paintType= PaintType.RECT;
        };
    }

    @Override
    public MenuClickListener ellipse() {
        return  e->{
            if(rootPanel.paintType==PaintType.ELLIPSE){
                rootPanel.paintType=PaintType.NULL;
            }else rootPanel.paintType= PaintType.ELLIPSE;
        };
    }

    @Override
    public MenuClickListener text() {
        return  e->{
            if(rootPanel.paintType==PaintType.TEXT){
                rootPanel.paintType=PaintType.NULL;
            }else rootPanel.paintType= PaintType.TEXT;
        };
    }

    @Override
    public MenuClickListener fillRect() {
        return e->{
            if(rootPanel.paintType==PaintType.FILL_RECT){
                rootPanel.paintType=PaintType.NULL;
            }else rootPanel.paintType= PaintType.FILL_RECT;
        };
    }

    @Override
    public MenuClickListener fillEllipse() {
        return e->{
            if(rootPanel.paintType==PaintType.FILL_ELLIPSE){
                rootPanel.paintType=PaintType.NULL;
            }else rootPanel.paintType= PaintType.FILL_ELLIPSE;
        };
    }
}
